package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 阀门状态读写界面
 * @author: 古霸天
 * @date: 2020/11/16
 */
public class SimpleSpinnerFragment extends AbstractProtocolFragment {
    private BluetoothCommunicationActivity activity;
    private CommunicationBean bean;
    private Spinner content;
    private TextView titleTxt;
    /**
     * 下拉框内容数组
     */
    private String[] names;
    /**
     * 下拉框对应内容16进制数组
     */
    private String[] codes;
    /**
     * 下拉框的标题
     */
    private String title;
    public SimpleSpinnerFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_simple_spinner, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        init(view);
    }

    /**
     * 初始化页面
     */
    private void init(View v) {
        final Map<String, Object> attr = bean.getAttr();
        title = attr.containsKey("title") && !StringUtils.isEmpty(attr.get("title").toString())
                ? attr.get("title").toString() : bean.getName();
        names = attr.containsKey("name") ? attr.get("name").toString().split(",") : new String[0];
        codes = attr.containsKey("code") ? attr.get("code").toString().split(",") : new String[0];

        content = v.findViewById(R.id.spinner_nb25_content);
        ArrayAdapter adapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, names);
        adapter.setDropDownViewResource(R.layout.dropdown_stytle);
        content.setAdapter(adapter);

        titleTxt = v.findViewById(R.id.spinner_nb25_title);
        titleTxt.setText(title + "：");

    }

    @Override
    public void fillFragment(String data) {
        String formatData = Nb25ProtocolUtils.formatReceiveData(data);
        reader(formatData);

    }

    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), getData(), PROTOCOL_VERSION);
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        int index = HexUtils.getArrayIndex(codes, data);
        if (index == -1) {
            Toast.makeText(activity, bean.getName() + "读取失败！", Toast.LENGTH_SHORT).show();
            return;
        }
        content.setSelection(HexUtils.getArrayIndex(codes, data));
    }

    /**
     * 获取写入数据
     * @return
     */
    private String getData() {
        return codes[content.getSelectedItemPosition()];
    }


}
